<?php

namespace app\common\model;

use basic\ModelBasic;

class Category extends ModelBasic
{
	use \traits\ModelTrait;

	 /**
     * 列表
     * @Author   FW
     * @DateTime 2020-09-24
     * @Email    123763712@qq.com
     * @param    [type]
     * @return   [type]
     */
    public static function getList($where){
        $default = [
            'cate_name' => '默认',
            'id'        => 0,
            'type'      => $where['type'] ?? 0,
            'contains'  => 0,
        ];

        $count = self::setWhere($where)->count();
        $data  = [];
        if ( $count == 0 ) {
            $data[] = $default;
            return compact('data','count');
        }

        $data=self::setWhere($where,'m')
        ->field('m.*')
        ->page($where['page'] ?? 1,$where['limit'] ?? 60)
        ->select();

        if ( !empty($data) ) {
            $data = $data->toArray();
            array_unshift($data , $default);
            $data = static::process_list($data);
        }

        return compact('data','count');
    }

    /**
     * 处理列表数据
     * @Author   FW
     * @DateTime 2021-01-12
     * @Email    123763712@qq.com
     * @param    array            $list [description]
     * @return   [type]                 [description]
     */
    public static function process_list (array $list)
    {
        foreach ($list as $key => &$item) {
            $item['contains'] = 0;
            ## 服务
            if ( $item['type'] == 2 ) {
                $item['contains']  = Service::where(['cate_id' => $item['id'],'is_del' => 0])->count();
            }
            ## 商品
            if ( $item['type'] == 3 ) {
                $item['contains']  = Goods::where(['cate_id' => $item['id'],'is_del' => 0])->count();
            }
        }
        return $list;
    }

	/**
	 * 设置where条件
	 * @Author   FW
	 * @DateTime 2021-01-12
	 * @Email    123763712@qq.com
	 * @param    [type]           $where [description]
	 * @param    string           $alias [description]
	 * @param    [type]           $model [description]
	 */
	public static function setWhere($where,$alias='m',$model=null){
        $model = $model===null ? new self() : $model;

        $model->alias($alias);

        if ( static::$softDelete ) {
        	$model->where($alias.'.is_del',0);
        }

        if ( isset($where['cate_name']) && $where['cate_name'] != '' ) {
            $model->where( sprintf('%s.cate_name' , $alias) . ' LIKE ' . "'%" . $where['cate_name'] ."%'");
        }

        if ( isset($where['type']) && $where['type'] > 0 ) {
            $model->where(sprintf('%s.type = %d' , $alias, $where['type']));
        }

        if ( isset($where['order']) && $where['order'] != '' ) {
            $model->order(sprintf('%s.%s' , $alias, $where['order']));
        } else {
            $model->order(sprintf('%s.id ASC' , $alias));
        }

        return $model;
    }
}//Class End
